'use strict';

const Controller = require('egg').Controller;
const { random } = require('lodash');

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    const ips = ctx.helper.getIPAdress();
    const room = random(1000, 9999);
    await ctx.render('pc/index.tpl', { ips, room });
  }

  async data() {
    try {
      const { ctx, app } = this;
      const { body } = ctx.request;

      const nsp = app.io.of('/');
      const msg = ctx.helper.parseMsg('pick', body.data);
      nsp.to(body.room).emit('pick', msg);
      ctx.body = { code: 200 };
    } catch (error) {
      console.log('error: ', error);
      app.logger.error(error);
    }
  }
}

module.exports = HomeController;
